package com.by.yz.handle;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.by.yz.App;
import com.by.yz.activity.MainActivity;
import com.by.yz.chatuidemo.ui.LoginActivity;
import com.by.yz.util.ExceptionUtils;
import com.by.yz.welcome.FirstWelcomeActivity;
import com.by.yz.welcome.WelcomeActivity;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

/**
 * Created by 你只能呵呵 on 2017/9/2.
 */

public class BaseExceptionHandler implements Thread.UncaughtExceptionHandler {
    /** 程序的Context对象 */
    private Context mContext;
    /** CrashHandler实例 */
    private static BaseExceptionHandler INSTANCE;

    //系统默认的UncaughtException处理类
    private Thread.UncaughtExceptionHandler mDefaultHandler;

    App application;

    /** 保证只有一个CrashHandler实例 */
    private BaseExceptionHandler() {

    }
    /** 获取CrashHandler实例 ,单例模式 */
    public static BaseExceptionHandler getInstance() {
        if (INSTANCE == null)
            INSTANCE = new BaseExceptionHandler();
        return INSTANCE;
    }

    /**
     * 初始化,注册Context对象, 设置该CrashHandler为程序的默认处理器
     *
     * @param ctx
     */
    public void init(Context ctx,App application) {
        mContext = ctx;
        this.application = application;
        Thread.setDefaultUncaughtExceptionHandler(this);
    }


    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        Writer resultWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(resultWriter);
        ex.printStackTrace(printWriter);
        StringBuffer sb = new StringBuffer();
        sb.append("是否在主线程中发生异常" + ExceptionUtils.isInMainThread() + "\n");
        sb.append("是否在主进程中发生异常" + ExceptionUtils.isMainProcess(App.mInstance) + "\n");
        String errorReport = sb.toString() + resultWriter.toString();
        //如果是主进程发生了异常 那还是交给系统自己默认的异常处理来做。让用户感知到，否则用户啥都不知道 体验并不好
        //你当然也可以自己定义特殊的错误提示 比如一些有意思的dialog什么的
        if (ExceptionUtils.isMainProcess(App.mInstance)) {
//            Toast.makeText(mContext, "很抱歉,程序异常退出，一秒后重新启动.",
//                    Toast.LENGTH_SHORT).show();
//            Log.e("ywx","主线程发生异常"+errorReport);
            mDefaultHandler.uncaughtException(thread, ex);
            Intent intent = new Intent(application.getApplicationContext(), FirstWelcomeActivity.class);
            PendingIntent restartIntent = PendingIntent.getActivity(
                    application.getApplicationContext(), 0, intent,
                    Intent.FLAG_ACTIVITY_NEW_TASK);
            //退出程序
            AlarmManager mgr = (AlarmManager)application.getSystemService(Context.ALARM_SERVICE);
            mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 0,
                    restartIntent); // 1秒钟后重启应用
            application.finishActivity();
        } else {
            //如果是子进程发生异常 不要给提示 直接殺掉子進程最好
            Log.e("ywx","子线程发生异常");
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }

}
